package polep.wespace.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.apache.log4j.Logger;

public class FileUtil {
	static Logger log = Logger.getLogger(FileUtil.class); 
	public static String getStr5File(String fileName){
		try {
			RandomAccessFile rf=new RandomAccessFile(fileName,"rw");
			StringBuffer strb=new StringBuffer();
			String str="";
			while((str=rf.readLine())!=null){
				strb.append(str);
			}
			rf.close();
			return new String(strb.toString().getBytes("UTF-8"));
		} catch (FileNotFoundException e) {
			log.error(e);
		} catch (IOException e) {
			log.error(e);
		}
		return null;
	}
	
	public static void putStr2File(String fileName,String str){
		try {
			RandomAccessFile rf=new RandomAccessFile(fileName,"rw");
			rf.write(str.getBytes());;
			rf.close();
		} catch (FileNotFoundException e) {
			log.error(e);
		} catch (IOException e) {
			log.error(e);
		}
		
	}
	
	public static void appendStr2File(String fileName,String str){
		try {
			RandomAccessFile rf=new RandomAccessFile(fileName,"rw");
			long length=rf.length();
			rf.seek(length);
			rf.write(str.getBytes());;
			rf.close();
		} catch (FileNotFoundException e) {
			log.error(e);
		} catch (IOException e) {
			log.error(e);
		}
	}
	
	public static boolean deleteFile(String filePath){
		File file = new File(filePath);
		if(file.exists()){
			return file.delete();
		}
		return false;
	}
	
	
}
